package com.campus.merchant.controller;

import cn.hutool.core.bean.BeanUtil;
import com.campus.common.bean.common.Resp;
import com.campus.merchant.bean.goods.GoodsDeleteBean;
import com.campus.merchant.bean.goods.GoodsSaveBean;
import com.campus.merchant.bean.goods.GoodsUpdateBean;
import com.campus.merchant.bean.goods.GoodsUpdateStatusBean;
import com.campus.merchant.entity.Goods;
import com.campus.merchant.service.GoodsService;
import com.campus.merchant.vo.goods.GoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "商品管理")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @ApiOperation("获取商品列表")
    @GetMapping("/list")
    public Resp<List<GoodsVO>> getGoodsList(
            @ApiParam("商户ID") @RequestParam Integer merchantId,
            @ApiParam("分类ID") @RequestParam(required = false) Integer categoryId,
            @ApiParam("商品状态") @RequestParam(required = false) Integer status) {
        return Resp.success(goodsService.getGoodsList(merchantId, categoryId, status));
    }

    @ApiOperation("批量更新商品状态")
    @PostMapping("/batchUpdateStatus")
    public Resp<Void> batchUpdateStatus(
            @Valid @RequestBody GoodsUpdateStatusBean bean) {
        goodsService.batchUpdateStatus(bean.getIds(), bean.getStatus());
        return Resp.success();
    }

    @ApiOperation("批量删除商品")
    @PostMapping("/batchDelete")
    public Resp<Void> batchDelete(
            @ApiParam("商品ID列表") @RequestBody GoodsDeleteBean bean) {
        goodsService.batchDelete(bean.getIds());
        return Resp.success();
    }

    @ApiOperation("获取商品详情")
    @GetMapping("/detail")
    public Resp<GoodsVO> getDetail(@RequestParam Integer id) {
        Goods goods = goodsService.getById(id);
        if (goods == null) {
            return Resp.error("商品不存在");
        }
        GoodsVO vo = new GoodsVO();
        BeanUtil.copyProperties(goods, vo);
        vo.setSpecJson(goodsService.getGoodsSpecJson(id));
        return Resp.success(vo);
    }

    @ApiOperation("新增商品")
    @PostMapping("/save")
    public Resp<Integer> save(@Validated @RequestBody GoodsSaveBean bean) {
        Goods goods = new Goods();
        BeanUtil.copyProperties(bean, goods);
        goodsService.saveGoodsWithSpecs(goods, bean.getSpecJson());
        return Resp.success(goods.getId());
    }

    @ApiOperation("编辑商品")
    @PostMapping("/update")
    public Resp<Void> update(@Validated @RequestBody GoodsUpdateBean bean) {
        Goods goods = new Goods();
        BeanUtil.copyProperties(bean, goods);
        goodsService.updateGoodsWithSpecs(goods, bean.getSpecJson());
        return Resp.success();
    }
}
